#!/usr/bin/env python
# headers.py -- SIP header objects
# @Author:      The Grugq (grugq@hcunix.net)

import sip
import values
from headerutils import normalise as _hn_normalise

class _MetaHeader(type):
    def curry(member, doc=None):
        def fget(self):
            return getattr(self.values[0], member)
        def fset(self, val):
            setattr(self.values[0], member, val)
            return getattr(self.values[0], member)
        def fdel(self):
            pass
        return property(fget,fset,fdel,doc)
    curry = staticmethod(curry) 

    def __new__(clsobj, clsname, bases, clsdict):
        if clsdict.has_key('__contents__'):
            for member, doc in clsdict['__contents__']:
                clsdict[member] = _MetaHeader.curry(member, doc)
        return type.__new__(clsobj, clsname, bases, clsdict)

# special kwargs parsing on __init__()
#   treat as params for first value.

class HeaderBase(sip.SIP):
    __metaclass__ = _MetaHeader
    HCOLON = ":"
    sep = ","
    def __init__(self, *args, **kwargs):
        self.name = ''
        self.values = []
        super(HeaderBase, self).__init__(*args)

        # XXX if not args and not kwargs:
        #   self.value[0] values.Value()
        if not len(self.values):
            self.values.append( self._vclass() )

        if kwargs and kwargs.has_key('params'):
            self.values[0].params.update( kwargs['params'] )

    def parse(self, buf):
        self.name, hvals = buf.split(self.HCOLON, 1)
        for hv in hvals.qsplit( self.sep ):
            self.values.append( self._vclass(hv) )

    def pack(self):
        return self.name + self.HCOLON + ' ' + \
                self.sep.join([str(v) for v in self.values])

class Token(HeaderBase):
    __contents__ = (
        ('value', 'An abitrary string value'),
    )
    _vclass = values.Token

class CallID(Token): pass
class String(Token): pass

class Expires(HeaderBase):
    __contents__ = (
        ('value', "A decimal value in seconds"),
    )
    _vclass = values.Expires

class Mime(HeaderBase):
    __contents__ = (
        ('type', 'Major MIME type'),
        ('sub_type', 'Minor MIME type'),
    )
    _vclass = values.Mime

class Int(HeaderBase):
    __contents__ = (
        ('value', "An integer value"),
        ('comment', "Optional comment"),
    )
    _vclass = values.Int

class GenericURI(HeaderBase):
    __contents__  = (
        ('uri', 'Generic URI'),
    )
    _vclass = values.GenericURI

class NameAddr(HeaderBase):
    __contents__ = (
        ('display_name', 'Printable name'),
        ('uri', 'Generic URI'),
    )
    _vclass = values.NameAddr

class CSeq(HeaderBase):
    __contents__ = (
        ('sequence', 'Sequence number'),
        ('method', 'SIP METHOD')
    )
    _vclass = values.CSeq

class Warning(HeaderBase):
    __contents__ = (
        ('code', 'Warning code'),
        ('hostname', 'Errant host system'),
        ('text', 'arbirary text tag')
    )
    _vclass = values.Warning

class Via(HeaderBase):
    __contents__ = (
        ('protocol', 'SIP'),
        ('version', '2.0'),
        ('transport', 'UDP'),
        ('host', 'localhost'),
        ('port', 0)
    )
    _vclass = values.Via

# XXX BELOW AREN'T WORKING YET!!!
class Auth(HeaderBase):
    __contents__ = (
        ('scheme', ''),
        ('algorithm', ''),
        ('cnonce', ''),
        ('nonce', ''),
        ('nc', ''),
        ('opaque', ''),
        ('qop', ''),
        ('realm', ''),
        ('response', ''),
        ('stale', ''),
        ('uri', ''),
        ('username', ''),
    )

    def _parse_value(self, buf):
        pass
    def _pack_value(self):
        pass

class Date(HeaderBase):
    pass
